package com.daipresents.gfmon;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

import org.apache.log4j.Logger;

public class ThreadMonitor implements ThreadMonitorMBean {

    private static final String SUCCESS = "success";
    
    private Logger logger = Logger.getLogger(ThreadMonitor.class);
    private ThreadMXBean bean;
    
    public ThreadMonitor() {
        super();
        logger.debug(ThreadMonitor.class.getName() + "Constractor Called.");
        
        bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    }
    
    @Override
    public String getLogging(){
        String message =
            String.format("CurrentThreadUserTime:%s,CurrentThreadCpuTime:%s,DaemonThreadCount:%s,PeakThreadCount:%s,ThreadCount:%s",
                    getCurrentThreadUserTime(),
                    getCurrentThreadCpuTime(),
                    getDaemonThreadCount(),
                    getPeakThreadCount(),
                    getThreadCount());
        logger.info(message);
        return SUCCESS;
    }
    
    @Override
    public long getCurrentThreadUserTime() {
        return bean.getCurrentThreadUserTime();
    }

    @Override
    public long getCurrentThreadCpuTime() {
        return bean.getCurrentThreadCpuTime();
    }

    @Override
    public int getDaemonThreadCount() {
        return bean.getDaemonThreadCount();
    }

    @Override
    public int getPeakThreadCount() {
        return bean.getPeakThreadCount();
    }

    @Override
    public int getThreadCount() {
        return bean.getThreadCount();
    }

}
